package net.asfun.jvalog.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.asfun.jvalog.domain.Tag;
import net.asfun.jvalog.http.RequestUtil;

public class TagAction extends BaseAction {

	public void index(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		this.assign("tags", Tag.listAll());
		this.render("admin/tags.html", resp);
	}
	
	public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Long[] ids = RequestUtil.getLongParams(req, "id");
		for(int i=0; i<ids.length; i++) {
			Tag.remove(ids[i]);
		}
		this.feedback("tag.delete", req, resp);
	}
	
	public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Long id = RequestUtil.getLongParam(req, "id");
		String name = RequestUtil.getRequiredParam(req, "name");
		String slogan =  RequestUtil.getRequiredParam(req, "slogan");
		Tag tag = Tag.load(id);
		tag.setName(name);
		tag.setSlogan(slogan);
		tag.update();
		this.feedback("tag.update", req, resp);
	}
	
	public void merge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Long id = RequestUtil.getLongParam(req, "id");
		Long mergeId = RequestUtil.getLongParam(req, "mergeTo");
		Tag tag = Tag.load(id);
		tag.mergeTo(mergeId);
		this.feedback("tag.merge", req, resp);
	}
	
	public void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Long id = RequestUtil.getLongParam(req, "id");
		this.assign("tag", Tag.load(id));
		this.render("admin/tag.html", resp);
	}
}
